<?php

namespace IDock\Insurance\HBIC\Response;

use Exception;
use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\HBIC\HBICEnums;
use IDock\Printer\Premium\PremiumBIPrinter;
use IDock\Printer\Premium\PremiumCIPrinter;
use IDock\Printer\Premium\PremiumCoveragePrinter;
use IDock\Printer\Premium\PremiumInquiryPrinter;
use IDock\Printer\Premium\PremiumTaxPrinter;
use IDock\Printer\PremiumUnderwritingPrinter;
use IDock\Printer\VehiclePrinter;

class InsuranceConfirmResponse extends AllHBICResponse
{
    public function printerInit(): Response
    {
        $premiumUnderwritingPrinter = new PremiumUnderwritingPrinter();

        $data = $this->getBody();
        if(($data['HEAD']['ERROR_CODE'] ?? '' ) != '0000'){
            //核保失败
            $premiumUnderwritingPrinter->statusCode = 'U2';
            $premiumUnderwritingPrinter->messageTip = $data['HEAD']['ERROR_MESSAGE'];
            $this->printer = $premiumUnderwritingPrinter;
            return $this;
        }
        $result = $data['BODY'] ?? [];
        $premiumUnderwritingPrinter->statusCode = 'U4';
        $underwriteInd = $result['BASE_PART']['PREUNDWRTRESULT'] ?? '7';
        $message = $result['BASE_PART']['AUDITDESCRIPTIONS']['AUDITDESCRIPTION'] ?? [];
        if(is_array($message)){
            $message = implode("\r\n",$message);
        }
        if(in_array($underwriteInd,['1','3','5'])){
            $premiumUnderwritingPrinter->statusCode = 'U1';//核保通过
        }else if($underwriteInd == '2' or $underwriteInd == '0'){
            $premiumUnderwritingPrinter->statusCode = 'U2';//退回修改
            $premiumUnderwritingPrinter->messageTip = $message ?: '退回修改';
        }else if($underwriteInd == '4'){
            $premiumUnderwritingPrinter->statusCode = 'U6';//拒保
        }else if($underwriteInd == '6'){
            $premiumUnderwritingPrinter->statusCode = 'U5';//已出保单
        }else if($underwriteInd == '8' or $underwriteInd == '9'){
            $premiumUnderwritingPrinter->statusCode = 'U3';//待审核
        }else if($underwriteInd == '7'){
            $premiumUnderwritingPrinter->statusCode = 'U4';//复核失败
        }
        $premiumUnderwritingPrinter->inquiryNo = $result['BASE_PART']['PROPOSALNO'] ?? '';
        $premiumUnderwritingPrinter->ciInquiryNo = $result['BASE_PART']['PROPOSALNOCI'] ?? '';
        $premiumUnderwritingPrinter->biInquiryNo = $result['BASE_PART']['PROPOSALNOBI'] ?? '';
        $this->printer = $premiumUnderwritingPrinter;
        return $this;
    }
}
